<template>
	<view class="uni-padding-wrap">
		<view class="myform">
			<view class="uni-form-item">
				<view class="uni-list-cell-left">
					国家/地区
				</view>
				<view class="uni-list-cell-db">
					<picker @change="bindPickerChange" :value="index" :range="array">
						<view class="uni-input">{{array[index]}}</view>
					</picker>
				</view>
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">手机号</view>
				<input class="uni-input" focus placeholder="请输入用户手机号" v-model="phone" />
				<text href="javascript:;" class="mygetcode" @click="getCode">{{message}}</text>
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">验证码</view>
				<input class="uni-input" focus placeholder="请输入用户验证码" v-model="code" />
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">密码</view>
				<input class="uni-input" password focus placeholder="请输入用户密码" v-model="password" />
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">确认密码</view>
				<input class="uni-input" password focus placeholder="请输入确认密码" v-model="repassword" />
			</view>
			<view class="mybtn"><button type="warn" @click="doreg">注册</button></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				array: ['中国', '美国', '巴西', '日本'],
				index: 0,
				phone: "",
				code:"",
				password:"",
				repassword:"",
				message: "获取验证码",
				time: this.helper.time
			};
		},
		onShow() {
			//uni.setNavigationBarTitle({
			//	title:"用户注册"
			//})
			//uni.setNavigationBarColor({
			//	backgroundColor:"#0A98D5",
			//	frontColor:"#ffffff"
			//})
		},
		methods: {
			bindPickerChange: function(e) {
				this.index = e.target.value;
			},
			daojishi() {
				let eventinterval = setInterval(() => {
					this.message = this.time-- + "秒";
					if (this.time <= 0) {
						clearInterval(eventinterval);
						this.message = "获取验证码";
					}
				}, 1000);
			},
			getCode() {
				if (this.message != "获取验证码") {
					return;
				}
				//短信验证码的获取
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(["phone=" + this.phone, "timestamp=" + timestamp]);
				this.helper.req({
					api:"getcode",
					timestamp:timestamp,
					data:{
						phone: this.phone
					},
					header:{
						sign: sign
					}
				}).then(res=>{
					if (this.message == "获取验证码") {
						this.time = this.helper.time;
						this.daojishi();
					}
				});
			},
			doreg() {
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['username='+this.phone,'code='+this.code,'password='+this.password,'repassword='+this.repassword,'timestamp='+timestamp]);
				this.helper.req({
					api:"users",
					timestamp:timestamp,
					data:{username:this.phone,code:this.code,password:this.password,repassword:this.repassword},
					header:{
						sign: sign
					}
				}).then(res=>{
					//注册成功
					uni.showToast({
						title: "注册成功",
						icon: "none",
						position: "bottom",
						success: () => {
							setTimeout(()=>{
								uni.navigateTo({
									url:"../login/login"
								});
							},1000)
						}
					})
				});
			}
		}
	}
</script>

<style lang="scss">
	.myform {
		.uni-form-item {
			border-bottom: 1px solid #DDD;

			.uni-list-cell-left {
				line-height: 76upx;
			}

			.mygetcode {
				color: darkgreen;
				line-height: 76upx;
				cursor: pointer;
			}
		}

		.mybtn {
			width: 612upx;
			margin: 0 auto;
			margin-top: 80upx;

			button {
				font-size: 36upx;
				height: 96upx;
				background-color: #3399FF;
			}
		}
	}
</style>
